<template>
  <div class="dfc mh-100p">
    <v-plain-table :data="tableData" @change="change" class="pd-10" :prefix="true">
      <template slot="prefix" slot-scope="{scope}">
        <span v-if="scope.row.isRead === 0" class="red"></span>
      </template>
      <template slot="expand" slot-scope="{scope}">
        <el-button type="text" @click="showDetail(scope.row)">详情</el-button>
        <el-button type="text" @click="deleteMessageById(scope.row.id)">删除</el-button>
      </template>
    </v-plain-table>
    <v-plain-dialog
            @close="getMessage"
            title="消息详情"
            width="40%"
            ref="detail">
      <el-form label-width="100px">
        <el-form-item label="消息标题">{{currentDetail.msgTitle}}</el-form-item>
        <el-form-item label="发送时间">{{currentDetail.createTime}}</el-form-item>
        <el-form-item label="消息内容">{{currentDetail.msgContent}}</el-form-item>
      </el-form>
      <p style="text-align: center;">
        <el-button type="primary" size="small" @click="closeDetail">关闭</el-button>
        <el-button type="primary" size="small" @click="checkDetail(currentDetail)">查看票据信息</el-button>
      </p>
    </v-plain-dialog>
  </div>
</template>

<script lang="ts">
  import { Vue, Component, Prop } from 'vue-property-decorator'
  import vFormTitle from '@/components/form-title'
  import vPlainTable from '@/components/plain-table'
  import vPlainDialog from '@/components/plain-dialog'
  import { Action } from 'vuex-class'

  @Component({
    components: {
      vFormTitle,
      vPlainTable,
      vPlainDialog
    }
  })
  export default class Bill extends Vue {
    tableData: TableData = {
      header: [
        {
          label: '标题',
          key: 'msgTitle'
        },
        {
          label: '内容',
          key: 'msgContent',
          formatter: row => row.msgContent.substr(0, 24) + '...'
        },
        {
          label: '发送时间',
          key: 'createTime'
        }

      ],
      body: [],
      footer: {
        total: 0,
        currentPage: 1
      }
    }
    currentDetail = {}

    async getMessage () {
      let res: AjaxResponse = await this.$axios.post('b/boms/message/notice/list', {
        currentPage: this.tableData.footer.currentPage,
        pageSize: 10,
        type: 1
      })
      this.tableData.body = res.data.items
      this.tableData.footer.total = res.data.totalNum
      this.getUnreadTips()
    }

    change (page) {
      this.tableData.footer.currentPage = page
      this.getMessage()
    }

    showDetail (row) {
      this.currentDetail = row
      this.readDetail(row.id)
      this.$refs.detail.openDialog()
    }

    readDetail (id) {
      this.$axios.post(`b/boms/message/notice/read/${id}`)
    }

    async deleteMessageById (id) {
      let res: AjaxResponse = await this.$axios.post(`b/boms/message/notice/delete/${id}`)
      this.$utils.tips(res)
      this.getMessage()
    }

    closeDetail () {
      this.$refs.detail.closeDialog()
    }
    checkDetail (row) {
      switch (row.msgType){
        case 4:
          this.$router.push('/transaction/inventory/list?type=0')
        break;
        case 5:
          this.$router.push('/transaction/offer/list?type=2')
        break;
      }
    }
    @Action getUnreadTips

    mounted () {
      this.getMessage()
    }
  }
</script>

<style lang="less" scoped>
  .red {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #E84F5A;
  }
</style>
